var SysConfig = function(){
	var configGridObj = null;
	var configValidator = null;
	function resetForm(rowIndex){
		AppUtils.clearForm("#configEdit");
		var record =  configGridObj.getRecord(rowIndex);
		$.each(record,function(key,value){
			$("#"+key).val(value);
		});
	}
	function saveConfigInfo(index){
		configValidator.form();
		if (!configValidator.valid()) {
			return false;
		}
		var configInfo = dwr.util.getFormValues('configEdit');
		SysConfigController.saveConfigInfo(configInfo,function(result){
			if(result){
				Message.success("保存成功！");
				configGridObj.refreshPage();
			}else{
				Message.error("保存失败！");
			}
			Message.close(index);
		});
	}
	return {
		initConfig:function(){
			configValidator=$("#configEdit").validate({
				rules:{
					configId:{required:true,maxlength:100},
					value:{required:true,maxlength:100},
					remark:{maxlength:100}
				}
			});
			configGridObj = $.fn.bsgrid.init('configTable', {
	            url:contextPath+"/sys/config/list.htm",
	            pageSizeSelect: true,
	            stripeRows: true,
	            rowSelectedColor:false,
	            pageSize: 10
				
	        });
		},
		
		Operate:function(record, rowIndex, colIndex, options){
			return '<a href="#" onclick="SysConfig.editConfigValue(\''+rowIndex+'\');">编辑</a>&nbsp;&nbsp;<a href="#" onclick="SysConfig.removeConfigValue(\''+configGridObj.getRecordIndexValue(record, 'configId')+'\');">删除</a>';
		},
		newConfigValue:function(){
			AppUtils.clearForm("#configEdit");
			DialogUtils.innerDialog({
				area: ['500px', '300px'],
			    title: '添加配置',
			    content: $("#editConfig"),
			    btn: ['保存', '取消'],
			    yes: function(index, layero){
			    	saveConfigInfo(index);
			    },cancel: function(index){
			    }
			});
		},
		editConfigValue:function(rowIndex){
			resetForm(rowIndex);
			DialogUtils.innerDialog({
				area: ['500px', '300px'],
			    title: '编辑配置',
			    content: $("#editConfig"),
			    btn: ['保存', '取消'],
			    yes: function(index, layero){
			    	saveConfigInfo(index);
			    },cancel: function(index){
			    }
			});
		},
		removeConfigValue:function(id){
			Message.confirm("确定删除该配置吗?",function(index){
				SysConfigController.removeConfigInfoById(id,function(result){
					if(result){
						Message.success("删除成功！");
						configGridObj.refreshPage();
					}else{
						Message.error("删除失败！");
					}
					Message.close(index);
				});
			});
		}
	}
}();